import pytest
from common.request_util_test import RequestUtil
from common.test_yaml_util_test import YamlUtil
from config import datas


class Testingress:
    url_unify = "/clusters/host"

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.ingress
    @pytest.mark.parametrize('ingressinfo',
                             YamlUtil().read_testcase_yaml('v25.06/resourcemanage/ingress/1_get_ingress_list.yaml'))
    def test_get_ingress_list(self, ingressinfo):
        # 获取ingress列表接口-正常
        base_url = datas.baseUrl + Testingress.url_unify
        url = base_url + ingressinfo['request']['url'].replace('{namespace}', 'fuyao-oauth')
        method = ingressinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == ingressinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.ingress
    @pytest.mark.parametrize('ingressinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/ingress/1_get_ingress_list_exception.yaml'))
    def test_get_ingress_list_exception(self, ingressinfo):
        # 获取ingress列表接口-异常
        base_url = datas.baseUrl + Testingress.url_unify
        url = base_url + ingressinfo['request']['url'].replace('{namespace}', 'fuyao-oauth')
        method = ingressinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == ingressinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.ingress
    @pytest.mark.parametrize('ingressinfo',
                             YamlUtil().read_testcase_yaml('v25.06/resourcemanage/ingress/2_create_ingress.yaml'))
    def test_create_ingress(self, ingressinfo):
        # 创建ingress接口-正常
        base_url = datas.baseUrl + Testingress.url_unify
        url = base_url + ingressinfo['request']['url'].replace('{namespace}', 'default')
        method = ingressinfo['request']['method']
        data = ingressinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        ingressname = req.json()['metadata']['name']
        globals()['ingressname'] = ingressname
        assert req.status_code == ingressinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.ingress
    @pytest.mark.parametrize('ingressinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/ingress/2_create_ingress_exception.yaml'))
    def test_create_ingress_exception(self, ingressinfo):
        # 创建ingress接口-异常
        base_url = datas.baseUrl + Testingress.url_unify
        url = base_url + ingressinfo['request']['url'].replace('{namespace}', 'default')
        method = ingressinfo['request']['method']
        data = ingressinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == ingressinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.ingress
    @pytest.mark.parametrize('ingressinfo',
                             YamlUtil().read_testcase_yaml('v25.06/resourcemanage/ingress/3_get_specific_ingress.yaml')
                             )
    def test_get_specific_ingress(self, ingressinfo):
        # 获取指定ingress接口-正常
        base_url = datas.baseUrl + Testingress.url_unify
        url = (base_url + ingressinfo['request']['url']
               .replace('{namespace}', 'default').replace('{ingressname}', globals()['ingressname']))
        method = ingressinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        updatereqbody = req.json()
        globals()['updatereqbody'] = updatereqbody
        assert req.status_code == ingressinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.ingress
    @pytest.mark.parametrize('ingressinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/ingress/3_get_specific_ingress_exception.yaml'))
    def test_get_specific_ingress_exception(self, ingressinfo):
        # 获取指定ingress接口-异常
        base_url = datas.baseUrl + Testingress.url_unify
        url = (base_url + ingressinfo['request']['url']
               .replace('{namespace}', 'default').replace('{ingressname}', globals()['ingressname']))
        method = ingressinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == ingressinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.ingress
    @pytest.mark.parametrize('ingressinfo',
                             YamlUtil().read_testcase_yaml('v25.06/resourcemanage/ingress/4_update_ingress.yaml'))
    def test_update_ingress(self, ingressinfo):
        # 更新指定ingress接口-正常
        base_url = datas.baseUrl + Testingress.url_unify
        url = (base_url + ingressinfo['request']['url']
               .replace('{namespace}', 'default').replace('{ingressname}', globals()['ingressname']))
        method = ingressinfo['request']['method']
        ingressinfo['request']['data'] = globals()['updatereqbody']
        data = ingressinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == ingressinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.ingress
    @pytest.mark.parametrize('ingressinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/ingress/4_update_ingress_exception.yaml'))
    def test_update_ingress_exception(self, ingressinfo):
        # 更新指定ingress接口-异常
        base_url = datas.baseUrl + Testingress.url_unify
        url = (base_url + ingressinfo['request']['url']
               .replace('{namespace}', 'default').replace('{ingressname}', globals()['ingressname']))
        method = ingressinfo['request']['method']
        ingressinfo['request']['data'] = globals()['updatereqbody']
        data = ingressinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == ingressinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.ingress
    @pytest.mark.parametrize('ingressinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/ingress/5_update_section_ingress.yaml'))
    def test_update_section_ingress(self, ingressinfo):
        # 部分更新指定ingress的接口-正常
        base_url = datas.baseUrl + Testingress.url_unify
        url = (base_url + ingressinfo['request']['url']
               .replace('{namespace}', 'default').replace('{ingressname}', globals()['ingressname']))
        method = ingressinfo['request']['method']
        data = ingressinfo['request']['data']
        header = ingressinfo['request']['headers']
        req = RequestUtil().send_request(method, url, headers=header, json=data)
        assert req.status_code == ingressinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.ingress
    @pytest.mark.parametrize('ingressinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/ingress/5_update_section_ingress_exception.yaml'))
    def test_update_section_ingress_exception(self, ingressinfo):
        # 部分更新指定ingress的接口-异常
        base_url = datas.baseUrl + Testingress.url_unify
        url = (base_url + ingressinfo['request']['url']
               .replace('{namespace}', 'default').replace('{ingressname}', globals()['ingressname']))
        method = ingressinfo['request']['method']
        data = ingressinfo['request']['data']
        header = ingressinfo['request']['headers']
        req = RequestUtil().send_request(method, url, headers=header, json=data)
        assert req.status_code == ingressinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.ingress
    @pytest.mark.parametrize('ingressinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/ingress/6_delete_specific_ingress.yaml'))
    def test_delete_specific_ingress(self, ingressinfo):
        # 删除ingress接口-正常
        base_url = datas.baseUrl + Testingress.url_unify
        url = (base_url + ingressinfo['request']['url']
               .replace('{namespace}', 'default').replace('{ingressname}', globals()['ingressname']))
        method = ingressinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == ingressinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.ingress
    @pytest.mark.parametrize('ingressinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/ingress/6_delete_specific_ingress_exception.yaml'))
    def test_delete_specific_ingress_exception(self, ingressinfo):
        # 删除ingress接口-异常
        base_url = datas.baseUrl + Testingress.url_unify
        url = (base_url + ingressinfo['request']['url']
               .replace('{namespace}', 'default').replace('{ingressname}', globals()['ingressname']))
        method = ingressinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == ingressinfo['validate'][0]['equals']['status_code']
